<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams1"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="状态" prop="jobResumeStatus">
        <el-select
          v-model="queryParams1.jobResumeStatus"
          placeholder="请选择"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.JOB_RESUME_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布职位" prop="jobId">
      <el-select
          v-model="queryParams1.jobId"
          placeholder="请选择"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="job in joblist"
            :key="job.jobId"
            :label="job.jobName"
            :value="job.jobId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['dbit:job-resume-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <div>
    <!-- 如果list为空，则显示提示信息 -->
   <div v-if="!list || list.length === 0" class="no-data">
    <img src="./none.png" alt="" width="100px"/>
     <p>暂无数据</p>
   </div>
     <!-- 卡片容器 -->
     <div v-else v-for="(item, index) in list" :key="index" class="card-container" @click="openForm(item)">
      <el-card>
        <div class="tag-container">
          <el-tag type="info" v-if="item.jobResumeStatus !== 1">已读</el-tag>
          <el-tag type="danger" v-else>未读</el-tag>
        </div>
        <div class="header-container">
          
          <div class="left-header">
            <div class="avatar-container">
              <el-avatar :size="70" :src="item.avatar" />
            </div>
            <div class="name-sex-edu">
              <p>
                {{ item.stuName }} - {{ item.sex === 1 ? '男' : item.sex === 2 ? '女' : '未知' }}</p>
              <dict-tag :type="DICT_TYPE.STU_EDU_TYPE" :value="item.stuEdu" /> - {{ item.graduationYear  }}
            </div>
          </div>
          <div class="center-header">
            <p>{{ item.universityFrom }}</p>
          </div>
          <div class="right-header">
            <div class="action-container" @click.stop>
              <!-- 当 jobResumeStatus 等于 0 或 1 时显示通过和不合适按钮 -->
              <template v-if="item.jobResumeStatus === 0 || item.jobResumeStatus === 1">
                <el-button
                  type="success"
                  @click="handleStatus(item.id, 2)"
                  v-hasPermi="['dbit:job-resume-info:delete']"
                >
                  通过
                </el-button>
                <el-button
                  type="danger"
                  @click="handleStatus(item.id, 3)"
                  v-hasPermi="['dbit:job-resume-info:delete']"
                >
                  不合适
                </el-button>
              </template>

              <!-- 当 jobResumeStatus 等于 2 时显示已通过 -->
              <template v-else-if="item.jobResumeStatus === 2">
                <el-text class="mx-1" type="success" size="large">通过</el-text>
              </template>

              <!-- 当 jobResumeStatus 等于 3 时显示不合适 -->
              <template v-else-if="item.jobResumeStatus === 3">
                <el-text class="mx-1" type="danger" size="large">不合适</el-text>
              </template>
            </div>
          </div>
        </div>
        <div class="footer-container">
          <div class="footer-left">
            <p>{{ formatTime(item.createTime) }} - {{ item.jobName }} </p>
          </div>
        </div>
      </el-card>
    </div>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams1.pageNo"
      v-model:limit="queryParams1.pageSize"
      @pagination="getList"
    />
  </div>

  <!-- 表单弹窗：添加/修改 -->
  <JobResumeInfoForm ref="formRef" @closed="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { JobResumeInfoApi, JobResumeInfoVO } from '@/api/dbit/jobresumeinfo'
import { JobInfoApi, JobInfoVO } from '@/api/dbit/jobinfo'
import JobResumeInfoForm from './JobResumeInfoForm.vue'
import {format} from 'date-fns'

/** 简历投递 列表 */
defineOptions({ name: 'JobResumeInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<JobResumeInfoVO[]>([]) // 列表的数据
const joblist = ref<JobInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams1 = reactive({
  pageNo: 1,
  pageSize: 10,
  jobResumeStatus: undefined,
  jobId:undefined
})
const queryParams2 = reactive({
  pageNo: 1,
  pageSize: undefined,
  jobStatus: 1,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中


const formatTime=(timestamp)=> {
      // 将时间戳转换为日期对象
      const date = new Date(timestamp)
      // 使用 date-fns 的 format 函数格式化日期
      return format(date, 'yyyy-MM-dd')
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await JobResumeInfoApi.getJobResumeInfoPage(queryParams1)
    const jobdata = await JobInfoApi.getJobInfoPage(queryParams2)
    console.log(data)
    list.value = data.list
    total.value = data.total
    joblist.value = jobdata.list
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams1.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const formRef = ref()
const openForm = (obj:any) => {
  formRef.value.open(obj)
}


const handleStatus = async (id: number,status:number) => {
  try {
    // 发起删除
    await JobResumeInfoApi.updateJobResumeInfoStatus(id,status)
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await JobResumeInfoApi.exportJobResumeInfo(queryParams1)
    download.excel(data, '简历投递.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped>
.card-container {
  display: flex;
  flex-direction: column;
  margin-bottom: 16px;
  cursor: pointer;
}

.header-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.left-header,
.right-header {
  display: flex;
  align-items: center;
}

.center-header {
  text-align: center;
}

.name-sex-edu {
  margin-left: 16px;
}

.footer-container {
  display: flex;
  align-items: flex-end;
  margin-top: auto;
}

.footer-left {
  margin-top: auto;
}

.avatar-container {
  margin-right: 16px;
}

.action-container {
  margin-left: auto;
}

/* 可能需要根据实际内容调整 */
.dict-tag {
  margin: 0 8px;
}

.no-data{
  text-align: center;
}

.tag-container {
  position: relative; /* 设置相对定位 */
}

.tag-container .el-tag {
  position: absolute; /* 设置绝对定位 */
  top: -19px;           /* 距离顶部的距离 */
  left: -20px;          /* 距离左边的距离 */
}
</style>
